package com.alks.function.service.impl.common;

import com.alks.common.aop.AutoPageAop;
import com.alks.common.data.thread.UserIdThread;
import com.alks.common.utils.beanutils.ArrayUtils;
import com.alks.entity.data.entity.PcStitchCollectDeptHead;
import com.alks.entity.data.entity.SdMtlNewKfRep;
import com.alks.entity.data.entity.SdVendor;
import com.alks.entity.data.enums.SlipTypeEnum;
import com.alks.entity.data.page.PageParam;
import com.alks.entity.data.page.PageRecord;
import com.alks.entity.data.response.ResponseInfo;
import com.alks.entity.exception.ServiceErrorException;
import com.alks.function.controller.finance.costcheck.LotMtlUsgPriceController;
import com.alks.function.data.dto.administration.PcEmployeeDto;
import com.alks.function.data.dto.chengKong.assort.SdMtlTypeNewDto;
import com.alks.function.data.dto.chengKong.cost.CpStitchProcessDetailDto;
import com.alks.function.data.dto.chengKong.produce.PcLotHeadDto;
import com.alks.function.data.dto.stockdept.pcfactorystockwork.LotComMtlDto;
import com.alks.function.data.request.back.MtlSubRequest;
import com.alks.function.data.request.procurement.SdVendorRequest;
import com.alks.function.data.request.stockdept.pcfactorystockwork.LotComMtlRequest;
import com.alks.function.mapper.MaterialMapper;
import com.alks.function.mapper.PcOrderHeadMapping;
import com.alks.function.mapper.administration.PcEmployeeMapper;
import com.alks.function.mapper.chengKong.PcStitchProdDetailMapper;
import com.alks.function.mapper.common.BackMapper;
import com.alks.function.service.common.BackService;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Service
@Slf4j
@RequiredArgsConstructor
public class BackServiceImpl implements BackService {
    private final BackMapper backMapper;

    private final PcEmployeeMapper pcEmployeeMapper;

    private final PcStitchProdDetailMapper pcStitchProdDetailMapper;
    @Autowired
    private LotMtlUsgPriceController lot;


    @Override
    public ResponseInfo vendorQuery() {
        String companyId = UserIdThread.get().getCompanyId();
        List<Map<String, String>> dtos = backMapper.vendorQuery(companyId);
        return ResponseInfo.ok(dtos);
    }

    /*厂商-全部查询（查询本公司的厂商和其他公司有而自己公司没有的厂商）*/
    @Override
    public ResponseInfo vendorAllQuery() {
        String companyId = UserIdThread.get().getCompanyId();
        List<Map<String, String>> dtos = backMapper.vendorAllQuery(companyId);
        return ResponseInfo.ok(dtos);
    }

    @Override
    public String getVendorNoByName(String vendorNo) {
        String dtos = backMapper.getVendorNoByName(vendorNo);
        return dtos;
    }

    @Override
    public ResponseInfo mtlTypeQuery() {
        List<Map<String, String>> dtos = backMapper.mtlTypeQuery();
        return ResponseInfo.ok(dtos);
    }

    @Override
    public ResponseInfo deptQuery() {
        List<Map<String, String>> dtos = backMapper.deptQuery();
        return ResponseInfo.ok(dtos);

    }

    @Override
    public ResponseInfo inSlipReasonQuery() {
        List<String> dtos = backMapper.inSlipReasonQuery();
        return ResponseInfo.ok(dtos);

    }

    @Override
    public ResponseInfo outSlipReasonQuery() {
        List<String> dtos = backMapper.outSlipReasonQuery();
        return ResponseInfo.ok(dtos);

    }

    @Override
    public ResponseInfo mtlTypeLMSQuery(String mtlType) {
        List<Map<String, String>> dtos = backMapper.mtlTypeLMSQuery(mtlType);
        return ResponseInfo.ok(dtos);
    }

    @Autowired
    private PcOrderHeadMapping pcOrderHeadMapping;
    @Override
    public ResponseInfo getAllOrderCondition() {
        Map<String, List<String>> map = new HashMap<>();
        List<String> brands = pcOrderHeadMapping.getOrderBrand();
        List<String> samples = pcOrderHeadMapping.getOrderSample();
        List<String> seasons = pcOrderHeadMapping.getOrderSeason();
        List<String> molds = pcOrderHeadMapping.getOrderMold();
        List<String> lasts = pcOrderHeadMapping.getOrderLast();
        map.put("brands",brands);
        map.put("samples",samples);
        map.put("seasons",seasons);
        map.put("molds",molds);
        map.put("lasts",lasts);
        return ResponseInfo.ok(map);
    }

    @Autowired
    private MaterialMapper materialMapper;
    @Override
    public ResponseInfo getDutyCondition() {
        Map<String, List<String>> map = new HashMap<>();
        List<String> dutyUsers = materialMapper.getDutyUsers();
        List<String> dutyRek = materialMapper.getDutyRek();
        map.put("dutyUsers",dutyUsers);
        map.put("dutyRek",dutyRek);
        return ResponseInfo.ok(map);
    }

    @Override
    public ResponseInfo getMtlType3() {
        List<Map<String, String>> type3 = backMapper.getMtlType3();
        return ResponseInfo.ok(type3);
    }

    @Override
    public ResponseInfo getMtlName3(String mtlName,String mtlNo,Integer pageNum,Integer pageSize) {
        Map<String, Object> map = new HashMap<>();
        PageHelper.startPage(pageNum, pageSize,true);
        List<SdMtlNewKfRep> list = backMapper.getMtlName3(mtlName,mtlNo);
        Page<SdMtlNewKfRep> page = (Page<SdMtlNewKfRep>) list;
        map.put("list",list);
        map.put("total",page.getTotal());
        return ResponseInfo.ok(map);
    }

    @Override
    public ResponseInfo getMtlUS1() {
        Map<String, List<String>> map = new HashMap<>();
        List<String> specs = backMapper.getMtlSpecs();
        List<String> units = backMapper.getMtlUnits();
        map.put("specs",specs);
        map.put("units",units);
        return ResponseInfo.ok(map);
    }

    @Override
    @AutoPageAop
    public ResponseInfo getEmployee(PageParam request,String deptName,String name) {
        System.out.println(deptName);
        List<Map<String,String>> dtos=backMapper.getEmployee(deptName,name);
        Page<Map<String,String>> page =(Page<Map<String, String>>) dtos;
        Map<String,Object> map = new HashMap<>();
        map.put("dtos",dtos);
        map.put("total",page.getTotal());
        return ResponseInfo.ok(map);
    }

    @Override
    public ResponseInfo vendorMoldQuery() {
        List<Map<String, String>> dtos = backMapper.vendorMoldQuery("G03");
        return ResponseInfo.ok(dtos);
    }

    @Override
    public ResponseInfo stockArea() {
        Map<String,Object> map = new HashMap<>();
        List<String> warehouseNos = backMapper.warehouseNos();
        warehouseNos.add("剩料仓");
        List<String> areaTypes = backMapper.areaTypes();
        map.put("warehouseNos",warehouseNos);
        map.put("areaTypes",areaTypes);
        return ResponseInfo.ok(map);
    }

    /**
     * 获取公司名称
     * @return
     */
    @Override
    public ResponseInfo getVendorCompanyName() {
        List<Map<String, String>> vendorNoByUserName = backMapper.getVendorNoByUserName();
        return ResponseInfo.ok(vendorNoByUserName);
    }

    @Override
    public ResponseInfo getVendorCompanyNameByCompanyId(String companyId) {
        String vendorNoByUserName = backMapper.getVendorCompanyNameByCompanyId(companyId);
        return ResponseInfo.ok(vendorNoByUserName);
    }

    /**
     * 材料类型回显-查询
     * @param typeNo
     * @return
     */
    @Override
    public ResponseInfo getMaterialType(String typeNo) {
        List<SdMtlTypeNewDto> sdMtlTypeNewDto = backMapper.getMaterialType(typeNo);
        return ResponseInfo.ok(sdMtlTypeNewDto);
    }

    /**
     * 指令型体回显-查询
     * @param lotNo
     * @return
     */
    @Override
    public ResponseInfo getLotNoAndStyle(String lotNo) {
        String companyId = UserIdThread.get().getCompanyId();
        List<PcLotHeadDto> sdMtlTypeNewDto = backMapper.getLotNoAndStyle(lotNo,companyId);
        return ResponseInfo.ok(sdMtlTypeNewDto);
    }

    /**
     * 指令回显以20分组-查询
     * @param lotNo
     * @return
     */
    @Override
    public ResponseInfo getLotNo(String lotNo,Integer pageSize, Integer pageNum) {
        PageHelper.startPage(pageNum,pageSize);
        String companyId = UserIdThread.get().getCompanyId();
        List<PcLotHeadDto> sdMtlTypeNewDto = backMapper.getLotNo(lotNo,companyId);

        PageInfo list = new PageInfo(sdMtlTypeNewDto);
        HashMap map = new HashMap<>();
        map.put("lotNoList", sdMtlTypeNewDto);
        map.put("total", list.getTotal());

        return ResponseInfo.ok(map);
    }

    /**
     * 费用类型查询
     * @param typeNo
     * @param typeName
     * @return
     */
    @Override
    public ResponseInfo getPcCostType(String typeNo, String typeName) {
        String companyId = UserIdThread.get().getCompanyId();
        List<Map<String, String>> costType = backMapper.getPcCostType(typeNo,typeName,companyId);
        return ResponseInfo.ok(costType);
    }

    /**
     * 根据条件查询所有厂商信息
     * @return
     */
    @Override
    public ResponseInfo getVendorByCondition(SdVendorRequest request) {
        String companyId = UserIdThread.get().getCompanyId();
        List<SdVendor> vendor = backMapper.getVendorByCondition(request,companyId);
        return ResponseInfo.ok(vendor);
    }

    @Override
    @AutoPageAop
    public List<String> getRyLots(String lotNo, PageParam request) {
        String companyId = UserIdThread.get().getCompanyId();
        List<String> lotNoList = backMapper.getRyLots(lotNo,companyId);
        return lotNoList;
    }

    @Override
    public void getRyLotCheck(String lotNo) {
        String companyId = UserIdThread.get().getCompanyId();
        int count = backMapper.getRyLotCount(lotNo,companyId,0,0);
        if (count==0){
            throw new ServiceErrorException("指令"+lotNo+"不存在");
        }if (count>1){
            log.warn("\n异常接口：getRyLotCheck\n异常原因：PC_LOT_HEAD出现重复指令");
            throw new ServiceErrorException("该正式指令不唯一");
        }
        count = backMapper.getRyLotCount(lotNo,companyId,1,0);
        if (count==0){
            throw new ServiceErrorException("指令"+lotNo+"未检核");
        }
    }

    @Override
    public void getRyLotCheck(List<String> lotNoList) {
        String companyId = UserIdThread.get().getCompanyId();
        if (ArrayUtils.isEmpyt(lotNoList)) {
            return;
        }
        List<String>  existLotList = backMapper.getExistRyLotList(lotNoList,companyId);
        if (ArrayUtils.isEmpyt(existLotList)){
            throw new ServiceErrorException("指令不存在");
        }
        List<String> miss = ArrayUtils.findMiss(existLotList, lotNoList);
        if (ArrayUtils.isNoEmpyt(miss)){
            throw new ServiceErrorException("指令"+miss.get(0)+"不存在");
        }
        List<String> disLotList = existLotList.stream().distinct().collect(Collectors.toList());
        if (disLotList.size()<existLotList.size()){
            throw new ServiceErrorException("存在重复指令");
        }
    }

    /**
     * 针车员工回显-查询
     * @param request
     * @param workName
     * @param workId
     * @return
     */
    @Override
    @AutoPageAop
    public ResponseInfo getStitchEmployee(PageParam request, String workName, String workId) {
        String companyId = UserIdThread.get().getCompanyId();
        //先查出针车部门编号
        List<PcStitchCollectDeptHead> dtos = backMapper.getStcColQuery2(companyId,null,null);
        List<String> depetList = dtos.stream().map(pcStitchCollectDeptHead -> pcStitchCollectDeptHead.getCollectDept()).collect(Collectors.toList());

        //根据部门去查询员工
        List<PcEmployeeDto> list = pcEmployeeMapper.getStitchEmployee(companyId,workName,workId,depetList);

        HashMap map = new HashMap<>();
        PageInfo page = new PageInfo(list);
        map.put("total",page.getTotal());
        map.put("list",list);
        return ResponseInfo.ok(map);
    }

    /**
     * 针车工序回显-查询
     * @param request
     * @param process
     * @return
     */
    @Override
    @AutoPageAop
    public ResponseInfo getStitchpeProcess(PageParam request, String process) {
        String companyId = UserIdThread.get().getCompanyId();
        //针车所有工序
        List<CpStitchProcessDetailDto> list = pcStitchProdDetailMapper.getAllCpStitchProcessDetail(companyId,process);

        HashMap map = new HashMap<>();
        PageInfo page = new PageInfo(list);
        map.put("total",page.getTotal());
        map.put("list",list);
        return ResponseInfo.ok(map);
    }

    /**
     * 针车组别回显-查询
     *
     * @param request
     * @param deptName
     * @param deptNo
     * @return
     */
    @Override
    @AutoPageAop
    public ResponseInfo getStitchpeDept(PageParam request, String deptName, String deptNo) {
        String companyId = UserIdThread.get().getCompanyId();
        //针车所有工序
        List<PcStitchCollectDeptHead> list = backMapper.getStcColQuery2(companyId,deptName,deptNo);

        HashMap map = new HashMap<>();
        PageInfo page = new PageInfo(list);
        map.put("total",page.getTotal());
        map.put("list",list);
        return ResponseInfo.ok(map);
    }

    @Override
    public ResponseInfo getSupplementDept(String searchKey) {
        List<Map<String,String>> list = backMapper.getSupplementDept(searchKey,UserIdThread.get().getCompanyId());
        return ResponseInfo.ok(list);
    }

    @Override
    @AutoPageAop()
    public PageRecord<Map<String,String>> getVendorSearch(String search, PageParam request) {
        List<Map<String,String>> list = backMapper.getVendorSearch(search,UserIdThread.get().getCompanyId());
        return new PageRecord<>(list);
    }

    @Override
    @AutoPageAop
    public PageRecord<String> getOrderId(PageParam request, String search) {
        List<String> list = backMapper.getOrderId(search,UserIdThread.get().getCompanyId());
        return new PageRecord<>(list);
    }

    @Override
    public ResponseInfo vendorByCI() {
        List<Map<String,Object>> list = backMapper.vendorByCI(UserIdThread.get().getCompanyId());
        return ResponseInfo.ok(list);
    }


    @Override
    public ResponseInfo getLots(String lot, Integer pageNum, Integer pageSize) {
        Map<String,Object> map = new HashMap<>();
        PageHelper.startPage(pageNum,pageSize);
        List<String> list = backMapper.getLots(lot);
        map.put("list",list);
        Page<String> page = (Page<String>) list;
        map.put("total",page.getTotal());
        return ResponseInfo.ok(map);
    }

    /*TODO 待完善*/
    @Override
    @AutoPageAop
    public ResponseInfo getVendorQuery(String vendorNo, String vendorName, PageParam request) {
        String companyId = UserIdThread.get().getCompanyId();
        List<Map<String,String>> dtos=backMapper.getVendorQuery(vendorNo,vendorName,companyId);
        Page<Map<String,String>> page=(Page<Map<String, String>>) dtos;
        Map<String,Object> map=new HashMap<>();
        map.put("dtos",dtos);
        map.put("total",page.getTotal());
        return ResponseInfo.ok(map);
    }

    @Override
    @AutoPageAop
    public ResponseInfo getMtlSub(MtlSubRequest request) {
        List<Map<String,String>> dtos=backMapper.getMtlSub(request);
        Page<Map<String,String>> page=(Page<Map<String, String>>) dtos;
        Map<String,Object> map=new HashMap<>();
        map.put("dtos",dtos);
        map.put("total",page.getTotal());
        return ResponseInfo.ok(map);
    }

    @Override
    public ResponseInfo getMtlType(Integer levelCode, String typeNo) {
        List<Map<String,String>> dtos=backMapper.getMtlType(levelCode,typeNo);
        return ResponseInfo.ok(dtos);
    }

    @Override
    public ResponseInfo getChangeOfOrderMakerList() {
        return ResponseInfo.ok(backMapper.getChangeOfOrderMakerList());
    }

    @Override
    public ResponseInfo getSizeTypeByName() {
        return ResponseInfo.ok(backMapper.getSizeTypeByName(UserIdThread.get().getCompanyId()));
    }

    @Override
    public ResponseInfo getSlipType() {
        return ResponseInfo.ok( SlipTypeEnum.SlipNameList());
    }

    @Override
    public ResponseInfo getStitchLine() {
        return ResponseInfo.ok(backMapper.getStitchLine());
    }

    @Override
    public ResponseInfo getStyleQuery(String style) {
        List<String> dtos=backMapper.getStyleQuery(style);
        Map<String,Object> map=new HashMap<>();
        map.put("dtos",dtos);
        return ResponseInfo.ok(map);
    }

    @Override
    public ResponseInfo getSlReasonQuery(String type) {
        List<Map<String,Object>> dtos= backMapper.getSlReasonQuery(type);
        return ResponseInfo.ok(dtos);
    }

    @Override
    @AutoPageAop
    public ResponseInfo getMtrQuery(String type, String mtlName, PageParam request) {
        List<Map<String,Object>> dtos=backMapper.getMtrQuery(type,mtlName,UserIdThread.get().getCompanyId());
        Page<Map<String,Object>> page=(Page<Map<String, Object>>) dtos;
        Map<String,Object> map=new HashMap<>();
        map.put("dtos",dtos);
        map.put("total",page.getTotal());
        return ResponseInfo.ok(map);
    }

    @Override
    public ResponseInfo getMtrTypeQuery() {
        List<Map<String,Object>> dtos=backMapper.getMtrTypeQuery();
        return ResponseInfo.ok(dtos);
    }

    @Override
    public ResponseInfo getMtrReasonQuery(String type) {
        List<String> dtos=backMapper.getMtrReasonQuery(type);
        return ResponseInfo.ok(dtos);
    }

    @Override
    public ResponseInfo getStcColQuery(Integer type) {
        String companyId = UserIdThread.get().getCompanyId();
        List<Map<String,Object>> dtos=backMapper.getStcColQuery(type,companyId);
        return ResponseInfo.ok(dtos);
    }

    @Override
    @AutoPageAop
    public ResponseInfo getComMtlQuery(LotComMtlRequest request) {
        /*获取数据*/
        List<LotComMtlDto> dtos = backMapper.getLotComMtlQuery(request);
        com.github.pagehelper.Page<LotComMtlDto> page = (com.github.pagehelper.Page<LotComMtlDto>) dtos;
        /*封装数据*/
        Map<String,Object> map = new HashMap<>();
        map.put("total", page.getTotal());
        map.put("dtos", dtos);
        return ResponseInfo.ok(map);
    }

    @Override
    public ResponseInfo getLineColQuery() {
        List<Map<String,Object>> dtos=backMapper.getLineColQuery();
        return ResponseInfo.ok(dtos);
    }

    @Override
    public ResponseInfo getBaseDetailQuery(String baseType, String itemName) {
        List<Map<String, Object>> dtos;
        if (baseType!=null&&!baseType.isEmpty()) {
            dtos = backMapper.getBaseDetailQuery(baseType,itemName, UserIdThread.get().getCompanyId());
        }else {
            dtos = backMapper.getBaseHeadQuery(UserIdThread.get().getCompanyId());
        }
        return ResponseInfo.ok(dtos);
    }

    @Override
    public ResponseInfo getLastTempQuery(String lastNo) {
        String companyId = UserIdThread.get().getCompanyId();
        List<String> dtos = backMapper.getLastTempQuery(lastNo,companyId);
        return ResponseInfo.ok(dtos);
    }

    @Override
    public ResponseInfo getYfMtlType3() {
        return  ResponseInfo.ok(backMapper.getYfMtlType3());
    }



}
